/**
 * Created by Administrator on 2017/3/22.
 */
const crypto = require('crypto');
/**
 * 非对称加密
 * @type {{verify: module.exports.verify, signer: module.exports.signer}}
 */
module.exports = {
    /**
     * 验签
     * @param sign
     * @param data
     */
    verify:function(sign, data){
        var publicKey = '-----BEGIN PUBLIC KEY-----\r\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwZMHQUgkMxQ3DFnoUsYggn/f1\r\nkscEeHPjsHWCUmzZ/8p8gbRNIm7RPDsYc1h9IyHma9OSb5Ihn6JfBKBIYhgN2XSa\r\noRCvAUoJnyryFbgV+wXx1u73Hlqp0noxhuxOqd+9jSmdbA0JRzbXmhsfucs6KM4t\r\noF4zR/pYvYUkzAUdNwIDAQAB\r\n-----END PUBLIC KEY-----\r\n';
        // var publick = new Buffer(publicKey,'base64');
        var verify = crypto.createVerify('RSA-SHA1');
        var decodedsig = new Buffer(sign, 'base64');
        verify.update(data,'utf8');
        return verify.verify(publicKey, decodedsig, 'hex');
    },
    /**
     * 生成sign
     * @param data
     */
    signer:function(data){
        var privateKey = '-----BEGIN RSA PRIVATE KEY-----\r\nMIICXAIBAAKBgQCwZMHQUgkMxQ3DFnoUsYggn/f1kscEeHPjsHWCUmzZ/8p8gbRN\r\nIm7RPDsYc1h9IyHma9OSb5Ihn6JfBKBIYhgN2XSaoRCvAUoJnyryFbgV+wXx1u73\r\nHlqp0noxhuxOqd+9jSmdbA0JRzbXmhsfucs6KM4toF4zR/pYvYUkzAUdNwIDAQAB\r\nAoGAcfxKOkIo0vWrlPu7XA/09ZAp/akhrkPgL6xh4xs8eAsRYC+4wXeUwOT5Ey3G\r\naub/CxV2nNQ4TD4KAPjOUrgA7i+JEtLI+yv0FeopU3gNPnJK3bvtcidCePG519Ku\r\nEygVtm6ulBQ/eSPGJhQYcoe+rhOK9qdAfn3bXwoa8d8hO2ECQQDVJR02Ojqr58tV\r\nDE098picbtaU5Uv+OBXFCrK9JSQl5PlKf6YRT3JqSxwh7jgk5mT+u3AdRPpG+dql\r\n9c1NPZCPAkEA09v+m+k9/AuPjiEHH7/XP1F7SCj+EaK9hOCV1xPKsP8XdaPkgGcd\r\nfJUDeXEQDo6+J//kJkp0Ht1IJpyatrMs2QJAYYu3v76MZWoSo1ya6RPihg0vxTrf\r\nOXwfcmr//BJIZdNRx6d9sARk/4JaYKjuJu0OIYpUuAB+bgcg6r0AdS4MvwJAQxth\r\nN9OWvbqPu0586jKjJLxTl5TcfYuWCnv1Go57WrtCnBHQ9Fc1QNwWoKYOlv3h/9uP\r\nUg2tYIVpMEnUu+BfaQJBAJZAM/v8tdxMlm32lhc4wy88SrR7toEEWBviuDH0ZncK\r\n7kbC8IHyC8j3ErNJytto6i0tUKBNen4Fa1p58CAoYFk=\r\n-----END RSA PRIVATE KEY-----\r\n';
        var sign = crypto.createSign('RSA-SHA256');
        sign.update(data);
        var sig =  sign.sign(privateKey,'base64');
        return sig;
    },
    cipher:function(data){
        var key = '-----BEGIN RSA PRIVATE KEY-----\r\nMIICXAIBAAKBgQCwZMHQUgkMxQ3DFnoUsYggn/f1kscEeHPjsHWCUmzZ/8p8gbRN\r\nIm7RPDsYc1h9IyHma9OSb5Ihn6JfBKBIYhgN2XSaoRCvAUoJnyryFbgV+wXx1u73\r\nHlqp0noxhuxOqd+9jSmdbA0JRzbXmhsfucs6KM4toF4zR/pYvYUkzAUdNwIDAQAB\r\nAoGAcfxKOkIo0vWrlPu7XA/09ZAp/akhrkPgL6xh4xs8eAsRYC+4wXeUwOT5Ey3G\r\naub/CxV2nNQ4TD4KAPjOUrgA7i+JEtLI+yv0FeopU3gNPnJK3bvtcidCePG519Ku\r\nEygVtm6ulBQ/eSPGJhQYcoe+rhOK9qdAfn3bXwoa8d8hO2ECQQDVJR02Ojqr58tV\r\nDE098picbtaU5Uv+OBXFCrK9JSQl5PlKf6YRT3JqSxwh7jgk5mT+u3AdRPpG+dql\r\n9c1NPZCPAkEA09v+m+k9/AuPjiEHH7/XP1F7SCj+EaK9hOCV1xPKsP8XdaPkgGcd\r\nfJUDeXEQDo6+J//kJkp0Ht1IJpyatrMs2QJAYYu3v76MZWoSo1ya6RPihg0vxTrf\r\nOXwfcmr//BJIZdNRx6d9sARk/4JaYKjuJu0OIYpUuAB+bgcg6r0AdS4MvwJAQxth\r\nN9OWvbqPu0586jKjJLxTl5TcfYuWCnv1Go57WrtCnBHQ9Fc1QNwWoKYOlv3h/9uP\r\nUg2tYIVpMEnUu+BfaQJBAJZAM/v8tdxMlm32lhc4wy88SrR7toEEWBviuDH0ZncK\r\n7kbC8IHyC8j3ErNJytto6i0tUKBNen4Fa1p58CAoYFk=\r\n-----END RSA PRIVATE KEY-----\r\n';
        var plaintext = new Buffer(data);
        var encrypted = "";
        var cipher = crypto.createCipher('aes192', key);
        encrypted += cipher.update(plaintext, 'binary', 'hex');
        encrypted += cipher.final('hex');
        return encrypted;
    },
    decipher:function (encrypted) {
        var decrypted = "";
        var key = '-----BEGIN RSA PRIVATE KEY-----\r\nMIICXAIBAAKBgQCwZMHQUgkMxQ3DFnoUsYggn/f1kscEeHPjsHWCUmzZ/8p8gbRN\r\nIm7RPDsYc1h9IyHma9OSb5Ihn6JfBKBIYhgN2XSaoRCvAUoJnyryFbgV+wXx1u73\r\nHlqp0noxhuxOqd+9jSmdbA0JRzbXmhsfucs6KM4toF4zR/pYvYUkzAUdNwIDAQAB\r\nAoGAcfxKOkIo0vWrlPu7XA/09ZAp/akhrkPgL6xh4xs8eAsRYC+4wXeUwOT5Ey3G\r\naub/CxV2nNQ4TD4KAPjOUrgA7i+JEtLI+yv0FeopU3gNPnJK3bvtcidCePG519Ku\r\nEygVtm6ulBQ/eSPGJhQYcoe+rhOK9qdAfn3bXwoa8d8hO2ECQQDVJR02Ojqr58tV\r\nDE098picbtaU5Uv+OBXFCrK9JSQl5PlKf6YRT3JqSxwh7jgk5mT+u3AdRPpG+dql\r\n9c1NPZCPAkEA09v+m+k9/AuPjiEHH7/XP1F7SCj+EaK9hOCV1xPKsP8XdaPkgGcd\r\nfJUDeXEQDo6+J//kJkp0Ht1IJpyatrMs2QJAYYu3v76MZWoSo1ya6RPihg0vxTrf\r\nOXwfcmr//BJIZdNRx6d9sARk/4JaYKjuJu0OIYpUuAB+bgcg6r0AdS4MvwJAQxth\r\nN9OWvbqPu0586jKjJLxTl5TcfYuWCnv1Go57WrtCnBHQ9Fc1QNwWoKYOlv3h/9uP\r\nUg2tYIVpMEnUu+BfaQJBAJZAM/v8tdxMlm32lhc4wy88SrR7toEEWBviuDH0ZncK\r\n7kbC8IHyC8j3ErNJytto6i0tUKBNen4Fa1p58CAoYFk=\r\n-----END RSA PRIVATE KEY-----\r\n';
        var decipher = crypto.createDecipher('aes192', key);
        decrypted += decipher.update(encrypted, 'hex', 'binary');
        decrypted += decipher.final('binary');
        var data = new Buffer(decrypted).toString();
        return data;
    }

};
function signer(data){
    var privateKey = '-----BEGIN RSA PRIVATE KEY-----\r\nMIICXAIBAAKBgQCwZMHQUgkMxQ3DFnoUsYggn/f1kscEeHPjsHWCUmzZ/8p8gbRN\r\nIm7RPDsYc1h9IyHma9OSb5Ihn6JfBKBIYhgN2XSaoRCvAUoJnyryFbgV+wXx1u73\r\nHlqp0noxhuxOqd+9jSmdbA0JRzbXmhsfucs6KM4toF4zR/pYvYUkzAUdNwIDAQAB\r\nAoGAcfxKOkIo0vWrlPu7XA/09ZAp/akhrkPgL6xh4xs8eAsRYC+4wXeUwOT5Ey3G\r\naub/CxV2nNQ4TD4KAPjOUrgA7i+JEtLI+yv0FeopU3gNPnJK3bvtcidCePG519Ku\r\nEygVtm6ulBQ/eSPGJhQYcoe+rhOK9qdAfn3bXwoa8d8hO2ECQQDVJR02Ojqr58tV\r\nDE098picbtaU5Uv+OBXFCrK9JSQl5PlKf6YRT3JqSxwh7jgk5mT+u3AdRPpG+dql\r\n9c1NPZCPAkEA09v+m+k9/AuPjiEHH7/XP1F7SCj+EaK9hOCV1xPKsP8XdaPkgGcd\r\nfJUDeXEQDo6+J//kJkp0Ht1IJpyatrMs2QJAYYu3v76MZWoSo1ya6RPihg0vxTrf\r\nOXwfcmr//BJIZdNRx6d9sARk/4JaYKjuJu0OIYpUuAB+bgcg6r0AdS4MvwJAQxth\r\nN9OWvbqPu0586jKjJLxTl5TcfYuWCnv1Go57WrtCnBHQ9Fc1QNwWoKYOlv3h/9uP\r\nUg2tYIVpMEnUu+BfaQJBAJZAM/v8tdxMlm32lhc4wy88SrR7toEEWBviuDH0ZncK\r\n7kbC8IHyC8j3ErNJytto6i0tUKBNen4Fa1p58CAoYFk=\r\n-----END RSA PRIVATE KEY-----\r\n';
    var sign = crypto.createSign('RSA-SHA1');
    sign.update(data);
    var sig =  sign.sign(privateKey,'base64');
    // var decodedsig = new Buffer(sig, 'base64');
    return sig;
}
function verify(sign, data){
    var publicKey = '-----BEGIN PUBLIC KEY-----\r\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwZMHQUgkMxQ3DFnoUsYggn/f1\r\nkscEeHPjsHWCUmzZ/8p8gbRNIm7RPDsYc1h9IyHma9OSb5Ihn6JfBKBIYhgN2XSa\r\noRCvAUoJnyryFbgV+wXx1u73Hlqp0noxhuxOqd+9jSmdbA0JRzbXmhsfucs6KM4t\r\noF4zR/pYvYUkzAUdNwIDAQAB\r\n-----END PUBLIC KEY-----\r\n';
    var verify = crypto.createVerify('RSA-SHA1');
    var decodedsig = new Buffer(sign, 'base64');
    verify.update(data,'utf8');
    return verify.verify(publicKey, decodedsig, 'hex');
}
//
//     function cipher(data) {
//         var key = 'MIICXAIBAAKBgQCwZMHQUgkMxQ3DFnoUsYggn';
//         var plaintext = new Buffer(data);
//         var encrypted = "";
//         var cipher = crypto.createCipher('aes192', key);
//         encrypted += cipher.update(plaintext, 'binary', 'hex');
//         encrypted += cipher.final('hex');
//         return encrypted;
//     }
//
//     function decipher(encrypted) {
//         var decrypted = "";
//         var key = 'MIICXAIBAAKBgQCwZMHQUgkMxQ3DFnoUsYggn';
//         var decipher = crypto.createDecipher('aes192', key);
//         decrypted += decipher.update(encrypted, 'hex', 'binary');
//         decrypted += decipher.final('binary');
//         var data = new Buffer(decrypted).toString();
//         return data;
//     }
//

console.log(signer('xiagang'));
// console.log(verify('CoB5UJ7UQY34yJ3r42NPPkFmL4v6W9S4wtMABsl7zI4VrwoFeMX6Mgb+yzdsuhXTknDWofHxQAf1+xDPKpEhOUjbXhrLyrrLfqgc9WLHM7N8OZULfDKhtRAmXhIQVS0t2gYwl7WNuu05lE/QF+2KChgKuum7KGEdCKoTC3vJ1VY=','123456'));



